// b2c/ad/ad.js
var tcity = require('../../js/Area.js');
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    cookie: null,
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false,
    falg: true,
    shr: "",
    phone: "",
    buttondefault: false,
    address: "",
    id:""
  },
  bindChange: function (e) {
    //console.log(e);
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;

    if (val[0] != t[0]) {
      const citys = [];
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i].name)
      }

      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0]
      })

      return;
    }
    if (val[1] != t[1]) {
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
      }

      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], val[1], 0]
      })
      return;
    }
    if (val[2] != t[2]) {
      this.setData({
        county: this.data.countys[val[2]],
        values: val
      })
      return;
    }


  },
  open: function () {
    this.setData({
      condition: !this.data.condition
    })
  },
  adhref() {
    const self = this;
    wx.navigateTo({
      url: '../map/map?longitudes=' + self.data.longitudes + '&latitudes=' + self.data.longitudes + '&text=' + self.data.hrefaddress
    })
  },
  /*收货人 */
  shrchange(e) {
    this.data.shr = e.detail.value;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this, a = wx.getStorageSync('cookie') || [];
    this.setData({
      cookie: a
    });
    this.mapCtx = wx.createMapContext('myMap')
    tcity.init(that);

    var cityData = that.data.cityData;


    const provinces = [];
    const citys = [];
    const countys = [];

    for (let i = 0; i < cityData.length; i++) {
      provinces.push(cityData[i].name);
    }
    for (let i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i].name)
    }
    for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i].name)
    }

    that.setData({
      'provinces': provinces,
      'citys': citys,
      'countys': countys,
      id:options.id
    })
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_edit_add",
      data: {
        id: options.id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + that.data.cookie
      },
      method: "POST",
      success: (res) => {
        console.log(res)
        const kk2 = res.data[0].addressList;
        that.setData({
          'province': kk2.prov,
          'city': kk2.city,
          'county': kk2.district,
          hrefaddress:kk2.address,
          lng:kk2.lng,
          lat:kk2.lat,
          phone:kk2.phone,
          shr:kk2.name,
          buttondefault:kk2.isuse,
          address: kk2.post
        })

      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })

  },
  phonechange(res) {
    this.data.phone = res.detail.value
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  swChange(res) {
    this.data.buttondefault = res.detail.value;
  },
  /*保存信息 */
  editor() {

    const self = this, data = self.data, reg = /^[0-9]*$/;
    if (encodeURIComponent(data.shr) == "" || encodeURIComponent(data.shr) == null || encodeURIComponent(data.shr) == undefined) {
      wx.showToast({
        title: '收货人不能为空！',
        icon: 'success',
        duration: 1500
      })
      return;
    } else if (!reg.test(+data.phone)) {
      wx.showToast({
        title: '手机号码只能是数字！',
        icon: 'success',
        duration: 1500
      })
      return;
    } else if (!data.address) {
      wx.showToast({
        title: '请填写收货地址',
        icon: 'success',
        duration: 1500
      })
      return;
    } else if (!data.hrefaddress) {
      wx.showModal({
        title: '提示',
        content: '未授权的无法获取收货坐标，需删除列表中的小程序再重新获取才能授权！',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return;
    }
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_add_address",
      data: {
        xgid: data.id,
        name: data.shr,
        hometel: "",
        phone: data.phone,
        prov: data.province,
        city: data.city,
        qu: data.county,
        address: data.hrefaddress,
        post: data.address,
        mrdz: data.buttondefault,
        lng: data.longitude,
        lat: data.latitude,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + data.cookie
      },
      method: "POST",
      success: (res) => {
        const json = JSON.parse(res.data);
        if (json.ret == "添加成功" || "修改成功") {
          app.globalData.nt = "2"
          wx.navigateBack()
        } else if (json.error == "最多只能设置6个地址，当前已经超出！") {
          wx.navigateBack()
        }
      },
      fail: (res) => {
        console.log(res)
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  addchange(res) {
    this.data.address = res.detail.value;
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      hrefaddress: app.globalData.hrefaddress,
      province: app.globalData.province,
      county: app.globalData.district,
      city: app.globalData.city,
      longitude: app.globalData.long,
      latitude: app.globalData.lat
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})